﻿using FluentValidation;
using WarehouseManagementSystem.DAL;
using WarehouseManagementSystem.Entities;

namespace WarehouseManagementSystem.Validators;

public class CargoValidator : AbstractValidator<Cargo>
{
    private readonly CargoRepository _cargoRepository;

    public CargoValidator(CargoRepository cargoRepository)
    {
        _cargoRepository = cargoRepository;

        RuleFor(x => x.Name).NotEmpty().MustAsync(IsExist).WithMessage("Name is not exist, can add");
        RuleFor(x => x.Tag).NotEmpty().WithMessage("Tag cannot be empty.");
    }

    private async Task<bool> IsExist(string name, CancellationToken cts) 
        => (await _cargoRepository.FindAll(name)).Any();

}